<template>
  <div class="app-container">

    <el-button type="primary" icon="el-icon-plus" size="mini" @click="open = true">清单商品</el-button>
    <el-table :data="goodsSelect" style="margin-top: 20px">
      <!-- 隐藏id列 -->
      <el-table-column prop="goodsid" width="100" ><template slot-scope="scope"><el-input type="hidden" v-model="scope.row.goodsid" /></template></el-table-column>
      <el-table-column prop="goodsName" label="商品名称" ></el-table-column>
      <el-table-column prop="goodsNum" isedit label="商品数量" ><div slot-scope="scope"><el-input v-on:blur="handleBlur(scope.row,scope.$index)" v-model="scope.row.goodsNum" /></div> </el-table-column>
      <el-table-column prop="goodsUnit"  label="单位" >
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_goods_unit" :value="scope.row.goodsUnit"/>
        </template>
      </el-table-column>

      <el-table-column prop="stock" label="当前库存" />
      <el-table-column prop="remark" label="备注" ><div slot-scope="scope"><el-input v-model="scope.row.remark" /></div> </el-table-column>

    </el-table>

    <el-form :model="form" ref="form" size="small" label-width="68px" style="bottom: 0;position: fixed;width: 100%;background-color: #fff;padding: 10px;">

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="submitForm">提交</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>


    <!-- 添加采购商品对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">

        <el-form-item label="采购商品"  prop="goods_name">
          <el-select v-model="goods" multiple filterable placeholder="请选择商品" >
            <el-option
              v-for="item in goodsList"
              :key="item.id"
              :label="item.name"
              :value="item.id+'|'+item.stock+'|'+item.price+'|' + item.name+'|'+item.unit"
            >
            </el-option>
          </el-select>
        </el-form-item>



      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="handleGoods">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {getListGoods,updateOrderGoods} from "@/api/system/outboundList"
import {listCompanyGoods} from "@/api/system/companyGoods"

export default {
  name: "goodsEdit",
  dicts: ['sys_goods_unit','sys_yes_no'],
  computed: {
    totalPrice() {
      let total = 0
      for (let i = 0; i < this.goodsSelect.length; i++) {
        const item = this.goodsSelect[i]
        total += item.price * item.goodsNum
      }
      return total
    }
  },
  data() {
    return {
      //采购单ID
      listid:"",

      //商品列表
      goodsList: [],
      //选中商品
      goods:"",
      //已选择商品列表
      goodsSelect:[],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 采购单表格数据
      orderlistList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 品类数量时间范围
      daterangeCtime: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        ctime: null,
        acceptTime: null,
        arriveTime: null,
        status: null,
        orderNo: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    }
  },
  //当获取焦点是触发
  activated() {
    console.log("==========")
    this.listid = this.$route.query.id
    console.log("编辑ID",this.listid)
    this.getGoodsList()
    this.getOrderGoods()
  },
  // mounted() {
  //   console.log("编辑ID",this.$router.history.current.query.id)
  //   this.listid = this.$router.history.current.query.id
  //   this.getOrderGoods()
  // },
  // created() {
  //   this.listid = this.$router.history.current.query.id
  // },
  methods: {
    //限制数量
    handleBlur(row,index){
      console.log(this.goodsSelect[index])
      if(row.goodsNum<0)this.goodsSelect[index].goodsNum=0
      if(row.goodsNum-0>row.stock-0)this.goodsSelect[index].goodsNum=row.stock
    },
    getOrderGoods(){
      getListGoods(this.listid).then(response => {
        this.goodsSelect = response.rows
        console.log("选中商品：",this.goodsSelect)
      })

    },
    //处理选中的商品
    handleGoods(){
      console.log("选中商品：",this.goods)
      this.goodsSelect = []
      for (let i = 0; i < this.goods.length; i++) {
        const item = this.goods[i]
        const detail = item.split("|")
        this.goodsSelect.push({"goodsid":detail[0],"goodsName":detail[3],"goodsNum":1,"stock":detail[1],"price":detail[2],"totalPrice":detail[2],"goodsUnit":detail[4]})
      }
      this.open = false
    },

    //获取商品列表
    getGoodsList(){
      listCompanyGoods({}).then(response => {
        this.goodsList = response.rows
        // console.log("goodslist:",this.goodsList)
      })
    },

    //删除该商品
    handleDelete(index){
      this.goodsSelect.splice(index, 1)
    },
    // 打开弹出层

    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        companyId: null,
        shopId: null,
        ctime: null,
        acceptTime: null,
        arriveTime: null,
        status: null,
        supplyId: null,
        orderNo: null,
        userId: null,
        price: null,
        remark: null,
        kind: null
      }
      this.resetForm("form")
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeCtime = []
      this.resetForm("queryForm")
      this.handleQuery()
    },
    /** 提交按钮 */
    submitForm() {
      let formdata = {}
      formdata.listid = this.listid
      formdata.goods = this.goodsSelect
      console.log("提交数据：",formdata)
      updateOrderGoods(formdata).then(response => {
        this.$alert("操作成功", "提示", {
          confirmButtonText: "确定",
          callback: action => {
            this.loading = false
            //关闭页面,返回列表页面
            this.$store.dispatch("tagsView/delView", this.$route)
            this.$router.go(-1)
          }
        })
      })
    },
  }
}
</script>
